<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Model\{Admin,Config}; 
#use App\Libraries\vendor\alipay\AopClient;
#use App\Libraries\vendor\alipay\request\AlipayFundTransToaccountTransferRequest;
class Alipay extends Model
{	
	private $appId = '2018060760295897';
	private $rsaPrivateKey = 'MIIEogIBAAKCAQEAwslkAQQupZwMBHX6gYAHaUfHPz3T8paWgfoQ/wundgAZjll8St9USdkBeOFkj1M8qTLnskJFdWhu67XIlkpPp8vR1svsPMHu4aPG6ReqxmBf8tWuLSflLQ6hcFlDEUYy0yANHdYm5kM2+Q9y542VQRH30kA/ReEX/l2vNyiy4+oQzmfgH/ITpC7VhWsJxqPLJQ85kY2J9hePZ5g1Rtw3klLtrDIFNTZoEGOpb8JVovTaKGYStIMCh66e4TTSsC8EXuiLz/hEhEXBgLsFwuGfYgrP8F6oBwrrIP3eGM8xjb8EN5Kn5bvP6L+h6MerU8PtL/dZ1dlp6OPpTfP31XxrUwIDAQABAoIBAGYJj4yEv1bbzuPLVcXe5oL9Kd/0KIIs5n0CHUMtvLSwyrJqKBUktofU2NOU5tO5284ar4sO8knVwj1RxdkM5ntU5wCpZIBTayriW4Ghd0FiHQEddJeNDZ2zy1gKOnrjFh7I7HYTx97ZmQORVzT7UxN6MSL/quWVS7dhJjcz57pB6IRR3qDw9YBgdNUSuUIWnzTJaHMU/SkGqETqb/ItUs6/IMRoWDUy1HC82J6XO06uaXIai2MKznxIqki0P2Sol1ObdmEGpjMf97mq1RfQVz8QgYHrWq6w7rD64rVHkgP9U4ue1vFRWMMvBIcGbtj/AQVc/eLe1qCjfQzFX16FniECgYEA/FwT3Mi/zqTM8BYI7jcJmmpk/Img/S4+fVOKwiYSB4t7+JXn3LKsyZI8E3ntBJnnWcuBJXsEhliQWhvJ16hz9rv9bt5Y7z4DsvoV0zdxiMRgO/h7Lz+XtDupOdssdvR+ZaVewGUlVR0HVqcqFPsHRq5HLf2EFzaiOx11x6j63DECgYEA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TNmjaZN6VLOMZRivi6rHHnSme8iw9h9xmV56RrLfGL2XVruu20tmQIDAQAB';

	/**
	 * 付款
	 * $p['out_biz_no']
	 * $p['payee_account']
	 * $p['amount']
	 * $p['payee_real_name']
	 * $p['uid']
	 * @return [type] [description]
	 */
	public function test($p){
		$aop = new AopClient();
		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->appId = '2018060760295897';
		$aop->rsaPrivateKey = 'MIIEogIBAAKCAQEAwslkAQQupZwMBHX6gYAHaUfHPz3T8paWgfoQ/wundgAZjll8St9USdkBeOFkj1M8qTLnskJFdWhu67XIlkpPp8vR1svsPMHu4aPG6ReqxmBf8tWuLSflLQ6hcFlDEUYy0yANHdYm5kM2+Q9y542VQRH30kA/ReEX/l2vNyiy4+oQzmfgH/ITpC7VhWsJxqPLJQ85kY2J9hePZ5g1Rtw3klLtrDIFNTZoEGOpb8JVovTaKGYStIMCh66e4TTSsC8EXuiLz/hEhEXBgLsFwuGfYgrP8F6oBwrrIP3eGM8xjb8EN5Kn5bvP6L+h6MerU8PtL/dZ1dlp6OPpTfP31XxrUwIDAQABAoIBAGYJj4yEv1bbzuPLVcXe5oL9Kd/0KIIs5n0CHUMtvLSwyrJqKBUktofU2NOU5tO5284ar4sO8knVwj1RxdkM5ntU5wCpZIBTayriW4Ghd0FiHQEddJeNDZ2zy1gKOnrjFh7I7HYTx97ZmQORVzT7UxN6MSL/quWVS7dhJjcz57pB6IRR3qDw9YBgdNUSuUIWnzTJaHMU/SkGqETqb/ItUs6/IMRoWDUy1HC82J6XO06uaXIai2MKznxIqki0P2Sol1ObdmEGpjMf97mq1RfQVz8QgYHrWq6w7rD64rVHkgP9U4ue1vFRWMMvBIcGbtj/AQVc/eLe1qCjfQzFX16FniECgYEA/FwT3Mi/zqTM8BYI7jcJmmpk/Img/S4+fVOKwiYSB4t7+JXn3LKsyZI8E3ntBJnnWcuBJXsEhliQWhvJ16hz9rv9bt5Y7z4DsvoV0zdxiMRgO/h7L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VovTaKGYStIMCh66e4TTSsC8EXuiLz/hEhEXBgLsFwuGfYgrP8F6oBwrrIP3eGM8xjb8EN5Kn5bvP6L+h6MerU8PtL/dZ1dlp6OPpTfP31XxrUwIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';

		$request = new AlipayFundTransToaccountTransferRequest();
		$content = "{" .
        "\"out_biz_no\":".$p['out_biz_no']."," .                     //订单号
        "\"payee_type\":\"ALIPAY_LOGONID\"," .
        "\"payee_account\":\"".$p['payment_account']."\"," .              //收款方账号
        "\"amount\":\"".$p['amount']."\"," .                               //金额
        "\"payer_show_name\":\"松鼠资讯APP\"," .           //付款方姓名
        "\"payee_real_name\":\"".$p['payee_real_name']."\"," .                 //收款方真实姓名
        "\"remark\":\"ID：".$p['uid']."收益提现\"" .                         //转账备注
        "}";
        $request->setBizContent($content);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        dd($responseNode);
	    $resultCode = $result->$responseNode->code;

	    if(!empty($resultCode)&&$resultCode == 10000){
	    	return ['status'=>1,'state'=>1];
	    }else{
	    	$sub_msg = $result->$responseNode->sub_msg;
        	$sub_code = $result->$responseNode->sub_code;
		    if( in_array($resultCode, ['20000', '20001', '40001', '40002','40006' ]) ){
		    	$state = 4;
		    	$message = $sub_msg;
		    }elseif($resultCode=='40004'){
		    	if( in_array($sub_code, ['PAYEE_NOT_EXIST', 'PERM_AML_NOT_REALNAME_REV','PAYEE_USER_INFO_ERROR','PAYEE_ACC_OCUPIED','PERMIT_NON_BANK_LIMIT_PAYEE','EXCEED_LIMIT_UNRN_DM_AMOUNT' ]) ){
		    		$state = 5;               
               		$message = $sub_msg;
		    	}else{
		    		$state = 4;
                	$message = $sub_msg;
		    	}
		    }
		    return ['status'=>0,'state'=>$state,'msg'=>$message];
		}
	}


	/**
	 * $p['out_biz_no'] 商户订单号
	 */
	public function get_info($p){
		$out_biz_no = $p['out_biz_no'];

		require_once app_path()."/../vendor/alipay/AopSdk.php";
		require_once app_path()."/../vendor/alipay/aop/AopClient.php";
		require_once app_path()."/../vendor/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php";
		$aop = new \AopClient ();
		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->appId = '2018060760295897';
		$aop->rsaPrivateKey = $this->rsaPrivateKey;
		$aop->alipayrsaPublicKey=$this->alipayrsaPublicKey;
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		$request = new \AlipayFundTransOrderQueryRequest ();
		$request->setBizContent("{" .
		"\"out_biz_no\":\"$out_biz_no\"" .
		"  }");
		return $result = $aop->execute ( $request); 
	}


	public function pay($p){
		$out_biz_no = $p['out_biz_no'];
		$payee_account = $p['payee_account'];
		$payee_real_name = $p['payee_real_name'];
		$amount = $p['amount'];

		require_once app_path()."/../vendor/alipay/AopSdk.php";
		require_once app_path()."/../vendor/alipay/aop/AopClient.php";
		require_once app_path()."/../vendor/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php";
		$aop = new \AopClient ();
		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
		$aop->appId = '2018060760295897';
		$aop->rsaPrivateKey = 'MIIEogIBAAKCAQEAwslkAQQupZwMBHX6gYAHaUfHPz3T8paWgfoQ/wundgAZjll8St9USdkBeOFkj1M8qTLnskJFdWhu67XIlkpPp8vR1svsPMHu4aPG6ReqxmBf8tWuLSflLQ6hcFlDEUYy0yANHdYm5kM2+Q9y542VQRH30kA/ReEX/l2vNyiy4+oQzmfgH/ITpC7VhWsJxqPLJQ85kY2J9hePZ5g1Rtw3klLtrDIFNTZoEGOpb8JVovTaKGYStIMCh66e4TTSsC8EXuiLz/hEhEXBgLsFwuGfYgrP8F6oBwrrIP3eGM8xjb8EN5Kn5bvP6L+h6MerU8PtL/dZ1dlp6OPpTfP31XxrUwIDAQABAoIBAGYJj4yEv1bbzuPLVcXe5oL9Kd/0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yziZ3c5ZDRRNa2pGDJaRxDPsV2AmSd+YwqiuFvDN0ad7wGAHL0hBWoPKLCneHY2ZNWmqDndQj1ACKp6m9JcRwlR1Rv0j5FaSIdZQBbgb/xRMuIy40Lgw2ZQNCdrPyN7g+M=';
		$aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwFs5WJMwaQ/whJf8QdIdPQ3NoSdUFnKfWY9itrau65Jlw1XlTZxfvLvMC/bnQefBomFbJsdwQJ5Oq3Vwf/8CpTCXFqO9fmtKf83/RtbQq16dLxqpzmsqGCK/sx0k8qRwQfTARl1aKDbDITARZL4QnGjwjvcGR06WWyUjZr/vPOsDb1Se3ZbMkG64GYP/bnlE+MSMFavKT8X6fxD9inW1qC0222HCeuCEZJk9zD7AJzSDWtIHq/1RACtXeIjJ8HQCZojjtr9k5FlDAWndGnytv1eSF7XLs3ncuTNmjaZN6VLOMZRivi6rHHnSme8iw9h9xmV56RrLfGL2XVruu20tmQIDAQAB';
		$aop->apiVersion = '1.0';
		$aop->signType = 'RSA2';
		$aop->postCharset='UTF-8';
		$aop->format='json';
		$request = new \AlipayFundTransToaccountTransferRequest ();
		$request->setBizContent("{" .
		"\"out_biz_no\":\"$out_biz_no\"," .
		"\"payee_type\":\"ALIPAY_LOGONID\"," .
		"\"payee_account\":\"$payee_account\"," .
		"\"amount\":\"$amount\"," .
		"\"payer_show_name\":\"松鼠资讯\"," .
		"\"payee_real_name\":\"$payee_real_name\"," .
		"\"remark\":\"收益提现\"" .
		"  }");
		$result = $aop->execute ( $request); 

		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$resultCode = $result->$responseNode->code;
		if(!empty($resultCode)&&$resultCode == 10000){
			return ['status'=>1,'state'=>1];
		} else {
			$sub_msg = $result->$responseNode->sub_msg;
        	$sub_code = $result->$responseNode->sub_code;
		    if( in_array($resultCode, ['20000', '20001', '40001', '40002','40006' ]) ){
		    	$state = 4;
		    	$message = $sub_msg;
		    }elseif($resultCode=='40004'){
		    	if( in_array($sub_code, ['PAYEE_NOT_EXIST', 'PERM_AML_NOT_REALNAME_REV','PAYEE_USER_INFO_ERROR','PAYEE_ACC_OCUPIED','PERMIT_NON_BANK_LIMIT_PAYEE','EXCEED_LIMIT_UNRN_DM_AMOUNT' ]) ){
		    		$state = 5;               
               		$message = $sub_msg;
		    	}else{
		    		$state = 4;
                	$message = $sub_msg;
		    	}
		    }
		    return ['status'=>0,'state'=>$state,'msg'=>$message,'order_id'=>$result->$responseNode->order_id];
		}
	}
}